package com.shanling.shanlingcontroller.ui.fragment;

import android.content.Intent;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseAppManager;
import com.shanling.shanlingcontroller.base.BaseMVPFragment;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.LoginBean;
import com.shanling.shanlingcontroller.bean.MybluetoothDevice;
import com.shanling.shanlingcontroller.persenter.LoginPersenter;
import com.shanling.shanlingcontroller.persenter.contract.LoginContract;
import com.shanling.shanlingcontroller.ui.activity.BleActivity;
import com.shanling.shanlingcontroller.ui.activity.ForgetActivity;
import com.shanling.shanlingcontroller.ui.activity.MainActivity;
import com.shanling.shanlingcontroller.utils.ToastUtils;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.litepal.LitePal;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ch1 on 2019/9/29.
 */

public class LoginFragment extends BaseMVPFragment<LoginContract.Presenter> implements LoginContract.View {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.iv_hide)
    ImageView ivHide;
    @BindView(R.id.tv_forget)
    TextView tvForget;
    @BindView(R.id.bt_login)
    Button btLogin;
    @BindView(R.id.iv_other)
    ImageView ivOther;
    @BindView(R.id.tv_other)
    TextView tvOther;
    @BindView(R.id.pb_login)
    ProgressBar pbLogin;
    private boolean isHide = true;
    private String username;
    private String psw;
    private IWXAPI api;

    @Override
    public void logingSuccess(LoginBean bean) {
        btLogin.setVisibility(View.VISIBLE);
        pbLogin.setVisibility(View.GONE);
        preferenceUtil.setSessionId(bean.getSessionId());
        preferenceUtil.setPassword(psw);
        preferenceUtil.setUserName(username);
        ToastUtils.showToast(getActivity(), R.string.login_success);
        List<MybluetoothDevice> mybluetoothDevices = LitePal.findAll(MybluetoothDevice.class);
        if(mybluetoothDevices.size()==0){
            readyGo(BleActivity.class);
        }else {
            readyGo(MainActivity.class);
        }
        getActivity().finish();
    }
    @Override
    public void loginFaild(String msg, String errocode) {
        btLogin.setVisibility(View.VISIBLE);
        pbLogin.setVisibility(View.GONE);
        ToastUtils.showToast(getActivity(), msg);
    }
    @Override
    public void getTokenSuccess(String appToken) {
        btLogin.setVisibility(View.VISIBLE);
        pbLogin.setVisibility(View.GONE);
        preferenceUtil.setAppToken(appToken);
    }
    @Override
    public void getTokenFaild(String msg) {
        btLogin.setVisibility(View.VISIBLE);
        pbLogin.setVisibility(View.GONE);
        ToastUtils.showToast(getActivity(), msg);
    }
    @Override
    protected LoginContract.Presenter createPresenter() {
        return new LoginPersenter();
    }

    @Override
    protected void onLazyLoad() {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_login;
    }
    @Override
    protected void initViewsAndEvents() {
        setHide(true);
        etPassword.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});
        if (TextUtils.isEmpty(preferenceUtil.getAppToken())) {
            mPresenter.getToken();
        }
        //通过WXAPIFactory工厂获取IWXApI的示例
        api = WXAPIFactory.createWXAPI(getActivity(),Constants.APP_ID_WX,true);
        //将应用的appid注册到微信
        api.registerApp(Constants.APP_ID_WX);
    }


    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }

    @OnClick({R.id.iv_hide, R.id.tv_forget, R.id.bt_login, R.id.iv_other})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_hide:
                isHide = !isHide;
                setHide(isHide);
                break;
            case R.id.tv_forget:
                readyGo(ForgetActivity.class);
                break;
            case R.id.bt_login:
                username = etPhone.getText().toString().trim();
                psw = etPassword.getText().toString().trim();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(psw)) {
                    ToastUtils.showToast(getActivity(), R.string.enter);
                    return;
                }
                btLogin.setVisibility(View.INVISIBLE);
                pbLogin.setVisibility(View.VISIBLE);
                if (TextUtils.isEmpty(preferenceUtil.getAppToken())) {
                    mPresenter.getToken();
                } else {
                    mPresenter.doLogin(username, psw);
                }
                break;
            case R.id.iv_other:
                if(!isWXAppInstalledAndSupported()){
                    ToastUtils.showToast(getActivity(),R.string.WeChat_notinstalled);
                }else {
                    SendAuth.Req req = new SendAuth.Req();
                    req.scope = "snsapi_userinfo";//
                    req.state = "wechat_sdk_wx_login";
                    api.sendReq(req);
                }
                break;
        }
    }

    public void setHide(boolean hide) {
        if (hide) {
            ivHide.setSelected(false);
            etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);//设置密码不可见
        } else {
            ivHide.setSelected(true);
            etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);//设置密码可见
        }
    }

    private boolean isWXAppInstalledAndSupported() {
        boolean sIsWXAppInstalledAndSupported = api.isWXAppInstalled();
        return sIsWXAppInstalledAndSupported;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();


    }


}
